<template>
  <div class="cascade-wrap" v-show="isshow">
    <el-row>
      <el-col :span="6">
        <div class="first-select">
          <label>一级地址：</label>
          <el-select
            :disabled="readonly"
            placeholder="省"
            v-model="Address.provinceCode"
            @change="firstLevelChangeHandler"
          >
            <el-option
              v-for="item in firstLevelAddressList"
              :key="item.addressCode"
              :label="item.addressName"
              :value="item.addressCode"
            >
            </el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="second-select">
          <label>二级地址：</label>
          <el-select
            :disabled="readonly"
            placeholder="市"
            v-model="Address.cityCode"
            @change="secondLevelChangeHandler"
          >
            <el-option
              v-for="item in secondLevelAddressList"
              :key="item.addressCode"
              :label="item.addressName"
              :value="item.addressCode"
            >
            </el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="third-select">
          <label>三级地址：</label>
          <el-select
            :disabled="readonly"
            placeholder="区/县"
            v-model="Address.areaCode"
            @change="thirdLevelChangeHandler"
          >
            <el-option
              v-for="item in thirdLevelAddressList"
              :key="item.addressCode"
              :label="item.addressName"
              :value="item.addressCode"
            >
            </el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="fourth-select">
          <label>四级地址：</label>
          <el-select
            :disabled="readonly"
            placeholder="街道"
            v-model="Address.countyCode"
            @change="fourthLevelChangeHandler"
          >
            <el-option
              v-for="item in fourthLevelAddressList"
              :key="item.addressCode"
              :label="item.addressName"
              :value="item.addressCode"
            >
            </el-option>
          </el-select>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapActions } from 'vuex';
export default {
  props: {
    /* 是否展示级联地址label */
    isshow: {
      type: Boolean,
      required: true
    },
    readonly: {
      type: Boolean,
      required: false
    },
    // 父组件传过来的四级
    // setSelectAddress: {
    //   type: Object,
    //   required: true,
    // },
  },
  data () {
    return {
      addressData: {},
      firstLevelAddressList: [], // 一级地址所有数据，省，直辖市
      secondLevelAddressList: [], // 二级地址所有数据， 市
      thirdLevelAddressList: [], // 三级地址所有数据， 区，县
      fourthLevelAddressList: [], // 四级地址所有数据， 街道，乡镇
      addressLevel: '', // 一到四级地址，分别对应 101 102 103 104
      Address: {
        provinceCode: '', // 一级地址，省，直辖市 CODE
        provinceId: '',
        provinceName: '',
        cityCode: '', // 二级地址， 市 CODE
        cityId: '',
        countyCode: '', // 四级地址， 区，县CODE
        countyId: '',
        cityName: '',
        countyName: '',
        areaCode: '', // 三 级地址 ， 街道，乡镇 CODE
        areaId: '',
        areaName: ''
      }
    };
  },
  computed: {},
  mounted () {},
  methods: {
    ...mapActions('sourcing/purchaseInquiry', [
      'getAddressListList' // 四级地址
    ]),
    setAddress (res) {
      this.Address = Object.assign({}, this.Address, {
        provinceCode: res.provinceCode,
        provinceId: res.provinceId,
        provinceName: res.provinceName,
        cityCode: res.cityCode,
        cityId: res.cityId,
        cityName: res.cityName,
        areaCode: res.areaCode,
        areaId: res.areaId,
        areaName: res.areaName,
        countyCode: res.countyCode,
        countyId: res.countyId,
        countyName: res.countyName
      });
      if (this.Address.provinceCode) {
        this.cascadeQueryAddressList('101', 0);
      }
      if (this.Address.cityCode) {
        this.cascadeQueryAddressList('102', Number(this.Address.provinceCode));
      }
      if (this.Address.areaCode) {
        this.cascadeQueryAddressList('103', Number(this.Address.cityCode));
      }
      if (this.Address.countyCode) {
        this.cascadeQueryAddressList('104', Number(this.Address.areaCode));
      }
    },
    /* 级联地址查询 */
    cascadeQueryAddressList (level, parentcode) {
      /* 接口入参addressLevel，分为101 102 103 104四个等级值 */
      const cb = (res) => {
        if (res.data.data) {
          switch (level) {
            case '101':
              this.firstLevelAddressList = res.data.data; // 获取一级地址数据
              break;
            case '102':
              this.secondLevelAddressList = res.data.data; // 获取二级地址数据
              break;
            case '103':
              this.thirdLevelAddressList = res.data.data; // 获取三级地址数据
              break;
            case '104':
              this.fourthLevelAddressList = res.data.data; // 获取四级地址数据
              break;
            default:
              break;
          }
        } else {
          this.$message.success(res.msg);
        }
      };
      const data = {
        addressLevel: level,
        parentAddressCode: parentcode
      };
      this.getAddressListList({
        payload: {
          data,
          cb
        }
      });
    },
    /* 选择一级地址时触发的事件函数 */
    firstLevelChangeHandler (parentcoded) {
      this.Address.provinceCode = parentcoded;
      this.firstLevelAddressList.map((item) => {
        if (item.addressCode == parentcoded) {
          this.Address.provinceName = item.addressName;
          this.Address.provinceId = item.addressId;
        }
      });
      this.cascadeQueryAddressList('102', parentcoded); // 查询二级地址数据
      this.Address.cityId = '';
      this.Address.cityCode = '';
      this.Address.cityName = '';
      this.secondLevelAddressList = [];
      this.Address.countyId = '';
      this.Address.countyCode = '';
      this.Address.countyName = '';
      this.thirdLevelAddressList = [];
      this.Address.areaId = '';
      this.Address.areaCode = '';
      this.Address.areaName = '';
      this.fourthLevelAddressList = [];
    },
    /* 选择二级地址时触发的事件函数 */
    secondLevelChangeHandler (parentcoded) {
      this.Address.cityCode = parentcoded;
      this.secondLevelAddressList.map((item) => {
        if (item.addressCode == parentcoded) {
          this.Address.cityName = item.addressName;
          this.Address.cityId = item.addressId;
        }
      });
      this.cascadeQueryAddressList('103', parentcoded); // 查询三级地址数据
      this.Address.countyId = '';
      this.Address.countyCode = '';
      this.thirdLevelAddressList = [];
      this.Address.areaId = '';
      this.Address.areaCode = '';
      this.Address.areaName = '';
      this.fourthLevelAddressList = [];
    },
    /* 选择三级地址时触发的事件函数 */
    thirdLevelChangeHandler (parentcoded) {
      this.Address.areaCode = parentcoded;
      this.thirdLevelAddressList.map((item) => {
        if (item.addressCode == parentcoded) {
          this.Address.areaName = item.addressName;
          this.Address.areaId = item.addressId;
        }
      });
      this.cascadeQueryAddressList('104', parentcoded); // 查询四级地址数据
      this.Address.countyId = '';
      this.Address.countyName = '';
      this.Address.countyCode = '';
      this.fourthLevelAddressList = [];
    },
    /* 选择四级地址时触发的事件函数 */
    fourthLevelChangeHandler (parentcoded) {
      this.Address.countyCode = parentcoded;
      this.fourthLevelAddressList.map((item) => {
        if (item.addressCode == parentcoded) {
          this.Address.countyName = item.addressName;
          this.Address.countyId = item.addressId;
        }
      });
    },
    getCheckedId () {
      return this.Address;
    },
    // 向父组件传值
    submitData () {
      return this.getCheckedId();
    }
  }
};
</script>
